package com.guaju.yueshangLife.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.guaju.yueshangLife.global.BaseApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chuangyejia on 2017/5/17.
 */

public class PreferenceUtil {

    private static final String POSITION = "positon";
    public static String GLOBAL_PREFERENCES = "dhtopnews_global";

    public static final String USER_ID = "user_id";
    public static final String TOKEN = "token";
    public static final String TOKEN_TYPE = "token_type";
    public static final String REFRESH_TOKEN = "refreshToken";
    public static final String AVATAR = "avatar";
    public static final String TEMP_AVATAR = "temp_avatar";
    public static final String FPHONE = "fphone";
    public static final String USER_NAME = "user_name";
    public static final String IS_LOGIN = "isLogin";
    public static final String IS_FIRSTIN = "isFirstIn";
    public static final String NICKNAME = "nickname";
    public static final String TERM = "term";
    public static final String VIDEO_CHANNEL = "video_channel";


    public static int getUserId() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getInt(USER_ID, 0);
    }

    public static void setUserId(int userId) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putInt(USER_ID, userId).commit();
    }

    public static String getToken() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(TOKEN, "");
    }

    public static void setToken(String token) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(TOKEN, token).commit();
    }


    public static String getTokenType() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(TOKEN_TYPE, "");
    }

    public static void setTokenType(String token) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(TOKEN_TYPE, token).commit();
    }

    public static String getRefreshToken() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(REFRESH_TOKEN, "");
    }

    public static void setRefreshToken(String token) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(REFRESH_TOKEN, token).commit();
    }

    public static void setNickName(String nickName) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(NICKNAME, nickName).commit();
    }

    public static String getNickName() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(NICKNAME, "");
    }

    public static void setPosition(String position) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(POSITION, position).commit();
    }

    public static String getPosition() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(POSITION, "");
    }
//    public static String getAvatar(){
//        String avatar = BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(AVATAR,"");
//        if (!TextUtils.isEmpty(avatar)){
//            return avatar + "&_rand=" + Utils.getTime();
//        }else {
//            return avatar;
//        }
//    }

    public static String getAvatar() {
        String avatar = BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(AVATAR, "");
//        if (!TextUtils.isEmpty(avatar)){
//            return avatar + "&_rand=" + Utils.getTime();
//        }else {
//            return avatar;
//        }
        return avatar + "?x-oss-process=image/resize,m_lfit,h_100,w_100";
    }

    public static void setTempAvatar(String token) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(TEMP_AVATAR, token).commit();
    }

    public static String getTempAvatar() {
        String avatar = BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(TEMP_AVATAR, "");
        return avatar;
    }

    public static void setAvatar(String token) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(AVATAR, token).commit();
    }

    public static String getFphone() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(FPHONE, "");
    }

    public static void setFphone(String token) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(FPHONE, token).commit();
    }


    public static String getUserName() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(USER_NAME, "");
    }

    public static void setUserName(String token) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(USER_NAME, token).commit();
    }
    public static void setTerm(String term) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putString(TERM, term).commit();
    }

    public static String getTerm() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getString(TERM, "");
    }

    public static boolean getIsLogin() {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getBoolean(IS_LOGIN, false);
    }

    public static void setIsLogin(boolean isLogin) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putBoolean(IS_LOGIN, isLogin).commit();
    }

    public static void putBoolean(String key, boolean bool) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putBoolean(key, bool).commit();
    }

    public static boolean getBoolean(String key) {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getBoolean(key, false);
    }

    public static void putLong(String key, Long l) {
        BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).edit().putLong(key, l).commit();
    }

    public static Long getLong(String key) {
        return BaseApplication.getInstance().getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE).getLong(key, 0);
    }



}
